package com.wingsoft.homeFlow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wingsoft.common.annotation.Log;
import com.wingsoft.common.enums.BusinessType;
import com.wingsoft.common.result.Result;
import com.wingsoft.homeFlow.bo.*;
import com.wingsoft.homeFlow.service.HomeFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping( "/flowTask")
public class HomeFlowTaskController {

    @Autowired
    private HomeFlowTaskService homeFlowTaskService;

    @PostMapping("/page")
    public Result<Page<HomeFlowTaskVo>> page(@RequestBody HomeFlowTaskQo homeFlowTaskQo) {
        return Result.success(homeFlowTaskService.page(homeFlowTaskQo));
    }

    @PostMapping("/get/{id}")
    public Result<HomeFlowTaskVo> getVoById(@PathVariable("id") String id) {
        return Result.success(homeFlowTaskService.getVoById(id));
    }

    @Log(title = "门户流水线任务新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<HomeFlowTaskVo> add(@RequestBody HomeFlowTaskAddBo homeFlowTaskAddBo) {
        return Result.success(homeFlowTaskService.add(homeFlowTaskAddBo));
    }

    @Log(title = "门户流水线任务更新", businessType = BusinessType.UPDATE)
    @PostMapping("/uptById")
    public Result<?> uptById(@RequestBody HomeFlowTaskUptBo homeFlowTaskUptBo) {
        homeFlowTaskService.uptById(homeFlowTaskUptBo);
        return Result.success();
    }

    @Log(title = "门户流水线任务更新状态", businessType = BusinessType.UPDATE)
    @PostMapping("/uptStatusById")
    public Result<?> uptStatusById(@RequestBody HomeFlowTaskUptBo homeFlowTaskUptBo) {
        homeFlowTaskService.uptStatusById(homeFlowTaskUptBo);
        return Result.success();
    }

    @Log(title = "门户流水线任务删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{id}")
    public Result<?> delById(@PathVariable("id") String id) {
        homeFlowTaskService.delById(id);
        return Result.success();
    }

    @Log(title = "门户流水线任务执行", businessType = BusinessType.UPDATE)
    @PostMapping("/execTask")
    public Result<HomeFlowTaskExecVo> execTask(@RequestBody HomeFlowTaskExecBo homeFlowTaskExecBo) {
        return Result.success(homeFlowTaskService.execTask(homeFlowTaskExecBo));
    }

    @Log(title = "门户流水线第三方任务执行", businessType = BusinessType.UPDATE)
    @PostMapping("/callTask")
    public Result<?> callTask(@RequestBody HomeFlowTaskExecBo homeFlowTaskExecBo) {
        HomeFlowTaskExecVo homeFlowTaskExecVo = homeFlowTaskService.execTask(homeFlowTaskExecBo);
        if (homeFlowTaskExecVo.getTaskLog().getSuccessFlag()) {
            return Result.success(homeFlowTaskExecVo.getResult());
        } else {
            return Result.failed(homeFlowTaskExecVo.getTaskLog().getErrorMsg());
        }
    }

    @PostMapping("/execTaskLog/{taskCode}")
    public Result<HomeFlowTaskExecTaskLogVo> getExecTaskLog(@PathVariable("taskCode") String taskCode) {
        return Result.success(homeFlowTaskService.getExecTaskLog(taskCode));
    }

}
